<?php
namespace app\home\model;

//加载配置类
use think\Config;
//导入Controller
use think\Model;
//导入Db
use think\Db;
//引入Session类
use think\Session;

class GoodsModel extends Model
{
	/**
	*  商品分类-顶级分类
	*/
	public function getParCate()
	{
		$result = Db::table('sz_category')->where('pid',0)->select();
		return $result;
	}

	/**
	*	商品分类-子类
	*   @param string $pid  商品子分类的父级id
	*/
	public function getCategory($pid)
	{
		$result = Db::table('sz_category')->where('pid',$pid)->select();
		$data = [];
		foreach($result as $key=>$val){
			$data[$key]['id']    = $val['id'];
			$data[$key]['name']  = $val['name'];
			$data[$key]['pic']   = getPreviewUrl($val['pic']);
			$data[$key]['path']  = $val['path'];
			$data[$key]['pid']   = $val['pid'];
		}

		return $data;
	}

	/**
	*	商品分类对应的商品列表
	*   @param string $cid  商品分类id
	*/
	public function getGoods($cid)
	{
		$where = [];

		/* 上架的商品 */
		$where['g_type'] = 2;
		$where['g_status'] = 2;
		$where['cid'] = $cid;
 		$result = Db::table('sz_goods')->where($where)->select();

 		$list = [];
 		$res  = [];
 		$data = [];
 		$aa   = [];
		foreach($result as $key=>$val){
			$list[$key] = json_decode($val['g_pic'],true);
			foreach($list as $k1=>$v1){
				$res[$key]['photos'] = $v1['photos'];
				foreach($res[$key]['photos'] as $k2=>$v2){
					$arr['photos'][$k2]['url'] = getPreviewUrl($v2['url']);
					$data[$key]['g_pic'] = json_encode($arr);
				}
			}

			$data[$key]['id']            = $val['id'];
			$data[$key]['cid'] 			 = $val['cid'];
			$data[$key]['g_type'] 		 = $val['g_type'];
			$data[$key]['g_name'] 		 = $val['g_name'];
			$data[$key]['g_cprice'] 	 = $val['g_cprice'];
			$data[$key]['g_oprice'] 	 = $val['g_oprice'];
			$data[$key]['g_description'] = $val['g_description'];
			$data[$key]['g_content']     = htmlspecialchars_decode($val['g_content']);
			$data[$key]['g_status']      = $val['g_status'];
			$data[$key]['g_show']        = $val['g_show'];
			$data[$key]['g_tnumber']     = $val['g_tnumber'];
			$data[$key]['g_number']      = $val['g_number'];
			$data[$key]['starttime']     = $val['starttime'];
			$data[$key]['endtime']       = $val['endtime'];
			$data[$key]['addtime']       = $val['addtime'];

		}

 		return $data;
	}


	/**
	*	商品详情页
	*   @param string $id  商品id
	*/
	public function getGoodsDetail($id)
	{
 		$res = Db::table('sz_goods')->where('id',$id)->find();

		$list = json_decode($res['g_pic'],true);
		$arr = [];
		foreach($list['photos'] as $k1=>$v1){
			$arr['photos'][$k1]['url'] = getPreviewUrl($v1['url']);
		}
		$res['g_pic'] = json_encode($arr);
		$res['g_content'] = htmlspecialchars_decode($res['g_content']);
		$res['lefttime'] = $res['endtime'] - strtotime(date('Y-m-d H:i:s',time()));

 		return $res;
	}

	/**
	*	获取某个商品对应的规格信息
	*   @param string $gid  商品id
	*/
	public function goodsInfo($gid)
	{
 		$res = Db::table('sz_info')->alias('i')
 			 ->join('sz_goods g','i.gid=g.id','left')
 			 ->field('i.*,g.g_unit')
 			 ->where('i.gid',$gid)
 			 ->select();

 		return $res;
	}

	/**
	*	点击加入购物车弹窗
	*   @param string $id  商品id
	*/
	public function getClickCart($id)
	{
		if($id){
			/* 该商品信息 */
			$result = Db::table('sz_goods')->where('id',$id)->find();
			$list = json_decode($result['g_pic'],true);
			$arr = [];
			foreach($list['photos'] as $k1=>$v1){
				$arr['photos'][$k1]['url'] = getPreviewUrl($v1['url']);
			}

			$result['g_pic'] = json_encode($arr);

			/* 该商品规格信息 */
			$res= Db::table('sz_info')->alias('i')
				->join('sz_goods g','i.gid=g.id','left')
				->field('i.*,g.g_unit')
				->where('gid',$id)
				->select();

			return ['goods'=>$result,'info'=>$res];
		}
	}

	/**
	*	获取单个商品分类
	*/
	public function getCategoryName($id)
	{
		if($id){
			$res = Db::table('sz_category')->where('id',$id)->find();
			return $res;
		}
	}
}